//
//  JQFYTabView.m
//  JQFY
//
//  Created by apple on 2017/8/6.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYTabView.h"
#import "JQFYTabButton.h"

@interface JQFYTabView()
@property (nonatomic, weak) UIButton *selectedButton;
@property (nonatomic,strong)UIImageView *tabbarImageView;
@property (nonatomic ,strong) UIButton *button;
@end
@implementation JQFYTabView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    
    UIImageView *tabbarImageView = [[UIImageView alloc]init];
    self.tabbarImageView = tabbarImageView;
    [self addSubview:tabbarImageView];
    [tabbarImageView setBackgroundColor:RGBCOLOR(70, 42, 132)];
    [tabbarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.equalTo(self);
    }];
    
    CGFloat y = 0;
    CGFloat w = self.bounds.size.width / 3;
    CGFloat h = self.bounds.size.height;
    for (int i = 1; i< 4; i++) {
        CGFloat x = (i-1) * w;
        CGRect frame = CGRectMake(x, y, w, h);
        _button = [[UIButton alloc]initWithFrame:frame];
        _button.tag = i;
        [self addSubview:_button];
        // 拼接图片的名称
        NSString *normal = [NSString stringWithFormat:@"TabBar%d", i];
        NSString *selected = [NSString stringWithFormat:@"TabBar%dSel", i];
        // 设置按钮的状态
        [_button setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
        [_button setImage:[UIImage imageNamed:selected] forState:UIControlStateSelected];
        [_button addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 1) {
            [self didClickBtn:_button];
        }
    }
    
    //监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didClickStart) name:@"didClickStartButton" object:nil];
}

-(void)didClickStart{

}

-(void)didClickBtn:(UIButton *)sender{
    
    // 让上一个被点击的按钮的selected状态变成NO
    self.selectedButton.selected = NO;
    // 让当前点击的按钮的状态变成YES
    sender.selected = YES;
    // 把当前点击的按钮记录到self.selectedButton中
    self.selectedButton = sender;
    // 2. 实现控制器的的切换
    if ([self.delegate respondsToSelector:@selector(bottomBarView:didClickBottomBarButton:)]) {
        [self.delegate bottomBarView:self didClickBottomBarButton:(int)sender.tag];
    }
    NSLog(@"%ld",sender.tag);
}

@end
